package com.blog.controller;

import com.blog.service.ArticleService;
import com.blog.service.CommentService;
import com.blog.service.impl.ArticleServiceImpl;
import com.blog.service.impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/DeleteCommentServlet.do")
public class DeleteCommentServlet extends HttpServlet {

    CommentService commentService=new CommentServiceImpl();
    ArticleService articleService =new ArticleServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理请求乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String articleId= req.getParameter("articleId");
        String commentId= req.getParameter("commentId");
        try {
            int count=commentService.deleteById(commentId);
            //评论数减一
            articleService.updateCommentCount("-",articleId);
            resp.getWriter().write("<script>alert('评论删除成功！');location.href='/Blog/ArticleDetailServlet.do?id="+articleId+"';</script>");
        } catch (IOException e) {
            e.printStackTrace();
            resp.getWriter().write("<script>alert('评论删除失败！');location.href='/Blog/ArticleDetailServlet.do?id="+articleId+"';</script>");
        }
    }
}
